package jhsys.hithink.basepage.flingview;

import java.util.ArrayList;
import java.util.List;

import jhsys.hithink.R;
import jhsys.hithink.db.HiDevice;
import jhsys.hithink.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DeviceAdapter extends BaseAdapter {

	public static final String TAG = "DeviceAdapter";
	private List<HiDevice> mList;
	private Context mContext;
	public static final int APP_PAGE_SIZE = 10;

	public DeviceAdapter(Context context, List<HiDevice> list, int page) {
		mContext = context;

		mList = new ArrayList<HiDevice>();
		int i = page * APP_PAGE_SIZE;
		int iEnd = i + APP_PAGE_SIZE;
		while ((i < list.size()) && (i < iEnd)) { // 最多取APP_PAGE_SIZE个图标
			mList.add(list.get(i));
			i++;
		}
	}
	
	public List<HiDevice> getList() {
		return mList;
	}

	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	private static final String PNG = ".png";
	private static final String PNG_00 = "00.png";
	private static final String PNG_01 = "01.png";
	private static final int STATE_CLOSE = 0;
	private static final int STATE_STOP = 0x80;
	private static final int STATE_UNKNOWN = 0x0a;
	private static final int STATE_CLOSE_IR = 0x0f;
	private static final int AIRCONTION_CLOSE = 0x50a0;
	private static final int AIRCONTION_UNKNOW = 0x50a8;

	public View getView(int position, View convertView, ViewGroup parent) {
		HiDevice device = mList.get(position);
		DeviceItem deviceItem;
		if (convertView == null) {

			View view = LayoutInflater.from(mContext).inflate(
					R.layout.app_item, null);

			deviceItem = new DeviceItem();
			// 分别用来存储图标和名字
			deviceItem.mDeviceIcon = (ImageView) view
					.findViewById(R.id.ivAppIcon);
			deviceItem.mDeviceName = (TextView) view
					.findViewById(R.id.tvAppName);

			view.setTag(deviceItem);
			convertView = view;
		} else {
			deviceItem = (DeviceItem) convertView.getTag();
		}

		Bitmap bm;
		int state = device.getState();
		if (state == STATE_CLOSE || state == STATE_CLOSE_IR
				|| state == STATE_UNKNOWN || state == AIRCONTION_CLOSE
				|| state == AIRCONTION_UNKNOW) {
			bm = Utils.getBitmap(getBitmapName(device.Icon) + PNG_00);
		} else if (state == STATE_STOP){
			bm = Utils.getBitmap(getBitmapName(device.Icon) + PNG_01);
		} else {
			bm = Utils.getBitmap(device.Icon);
		}
		deviceItem.mDeviceIcon.setImageBitmap(bm);
		deviceItem.mDeviceName.setText(device.Name);

		return convertView;
	}
	
	private String getBitmapName(String iconName) {
		String btmName = "";
		if (iconName.endsWith(PNG)) {
			btmName = iconName.replaceAll(PNG, "");
		}
		return btmName;
	}

	class DeviceItem {
		ImageView mDeviceIcon;
		TextView mDeviceName;
	}

}
